<?php
/**
 * 根据service自动填写数据并做CURD操作[仅对单表做操作]
 * @author vincent , wcf 1.0 
 *
 */
class Mediad2_Service_Util_M 
{
	/**
	 * var oservice
	 * @var Mediad2_Service_Abstract
	 */
	private $oservice;
	private $metadata;
	private $model;
	private $pk = null;
	private $isdebug = true;//调试模式
	private $help;
	
	public function __construct($obj,$model=null)
	{
		$this->oservice = $obj;
		$p = $obj->getMapper()->getPk();
		$this->pk = $p[1]; //TODO:有可能存在1个以上的主键
		if (empty($model)) {
			$this->model = $this->oservice->getModel();
		} else {
			$this->model = $model;
		}
		$mo = $this->oservice->getMapper();
		$dao = $mo->getDao();
		$this->metadata = $dao->info('metadata');
		if($this->isdebug)
			$this->help=new Mediad2_Utility_SmartyHelper();
	}
	
	/**
	 * create 
	 * @param array $request
	 */
	public function create(array $request=null) 
	{
		
		//从GET/POST数据中抓取数据并保存在model中
		if(empty($request)){
			$request = $_REQUEST;
		}
		
		$meta = array();
		
		foreach($this->metadata as $key=>$val){//获取所有字段名称，类型，非空属性
			if($val['PRIMARY'])
				continue;
			$isnull = 0 ;
			if($val['NULLABLE']==false)
				$isnull = 1 ;
			$meta[] = array("name"=>$key,"type"=>$val['DATA_TYPE'],"isnull"=>$isnull);
		}
		foreach ($request as $key=>$var) {//根据客户端传的参数对应赋值
			foreach ($meta as $index => $value) {
				if($key == $meta[$index]['name']){
					if(!isset($request[$key])) continue;//必填字段必须有值且不能为空字符串					
					$meta[$index]['data'] = $request[$key];
				}
			}
		}
	
		$modelAry = array();
		foreach ($meta as $key => $value) {
			$arg = @$meta[$key]['data'];
			if($meta[$key]['isnull']==1 &&  !isset($arg)){//判断必填项
				if($this->isdebug){
					//echo "<pre>";					
					//echo $meta[$key]['name']."必填字段";
					$this->help->log(sprintf("%s是必填字段.",$meta[$key]['name']));
				}
				return false;
			}
			$modelAry[$meta[$key]['name']]= @$meta[$key]['data'];
		}		
		//如何存在PK值，将PK值保存在model中
		$obj_pk = @$request[$this->pk];
		if(isset($obj_pk)){
			$modelAry[$this->pk] = $obj_pk;
		}
			
		$this->model->setOptions($modelAry);
		return true;
	}
	
	/**
	 * add
	 * @param mixed $data
	 */
	public function add($data = null){
		return $this->oservice->insert($this->model);
	}
	
	/**
	 * save
	 * @param mixed $data
	 */
	public function save($data = null){
		if(!isset($this->pk))return false;
		
		if($data!=null){
			return $this->oservice->update($data);
		}
			
		//将传入的数据替换到update模板中,并保存更新
		$source 	= $this->model->toArray();
		$objResult 	= $this->oservice->find($source[$this->pk]);
		$objmodel 	= $objResult->getData();
		$target 	= $objmodel->toArray();
		foreach ($source as $key => $value) {
			$target[$key] = $source [$key];
			if($this->isdebug){
				//echo sprintf("key:%s,value:%s.",$key,$source[$key]);
				$this->help->log(sprintf("[%s:%s]",$key,$source[$key]));
			}
		}
		$objmodel = $this->model->setOptions($target);
		return $this->oservice->update($objmodel);
	}
	
	/**
	 * delete
	 * @param int $id
	 */
	public function delete($id){
		return $this->oservice->delete($id);
	}
	
}